import { CopyControllerBase } from "./CopyControllerBase";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SMagicTowerInfo } from "../../../../../Message/Public/SMagicTowerInfo";
import { MagicTowerInfoPanel } from "../magicTower/MagicTowerInfoPanel";
import { GuideType } from "../../../rules/GuideType";
import { ETaskStatus } from "../../../../../Message/Game/ETaskStatus";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { MagicTowerAutoPath } from "../magicTower/MagicTowerAutoPath";
import { SStory } from "../../../../../Message/Public/SStory";
import { MsgManager } from "../../../manager/MsgManager";
import { MagicTowerConfig } from "../../../resource/MagicTowerConfig";
import { GameController } from "../../../mvc/GameController";
import { CopyConfig } from "../../../resource/CopyConfig";
import { Language } from "../../../../../extend/language/Language";
import { CopyUtil } from "../CopyUtil";
import { ItemData } from "../../../resource/info/ItemData";
import { GameProxy } from "../../../mvc/GameProxy";
import { EPlayerItemPosType } from "../../../../../Message/Public/EPlayerItemPosType";
import { DelegateSettingRule } from "../../delegate/warehouse/DelegateSettingRule";
import { IView } from "../../../../mvc/interfaces/IView";
import { TowerCopyView } from "../magicTower/TowerCopyView";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { GModuleEvent } from "../../../mvc/GModuleEvent";
//class TowerCopyController
    
    export  class TowerCopyController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */onReceiveMagicTowerInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SMagicTowerInfo;
            var loc2=cache.copy.magicTowerInfo && !(cache.copy.magicTowerInfo.floor == loc1.floor) ? true : false;
            cache.copy.magicTowerInfo = loc1;
            if (loc1.floor > 0 && loc1.killBossNum == loc1.totalBossNum) 
            {
                cache.copy.magicTowerPassedFloor = loc1.floor;
            }
            else if (loc1.floor == 1) 
            {
                cache.copy.magicTowerPassedFloor = 0;
            }
            if (!this._magicTowerInfoPanel) 
            {
                this._magicTowerInfoPanel = new MagicTowerInfoPanel();
                if (cache.task.hasTaskByGuide(GuideType.GuideTower, ETaskStatus._ETaskStatusNotCompleted)) 
                {
                    this._magicTowerInfoPanel.updateStatus(MagicTowerInfoPanel.Shrink);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
                }
                else 
                {
                    this._magicTowerInfoPanel.updateStatus(MagicTowerInfoPanel.Expand);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                }
                this._magicTowerInfoPanel.updatePosition();
                this._magicTowerInfoPanel.show();
            }
            this._magicTowerInfoPanel.updateInfos(loc2);
            if (loc2 && loc1.floor <= 0) 
            {
                MagicTowerAutoPath.instance.startCountDown();
            }
            return;
        }

        /* internal  */onShowMagicTowerStory(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SStory;
            MsgManager.showTaskTarget(loc1.text, 8);
            return;
        }

        /* internal  */onChangeSceneHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (cache.copy.isInMagicTower()) 
            {
                loc1 = MagicTowerConfig.instance.getInfoByFloor(cache.copy.magicTowerInfo.floor);
                if (loc1 && !(loc1.messageInfo == "")) 
                {
                    MsgManager.showTaskTarget(loc1.messageInfo, 8);
                    GameController.chat.addTypeRumorMsg(loc1.messageInfo, []);
                }
            }
            return;
        }

        /* internal  */onLeaveMagicTowerCopy(arg1: SMagicTowerInfo): void
        {
            if (this._magicTowerInfoPanel) 
            {
                this._magicTowerInfoPanel.hide();
                this._magicTowerInfoPanel = null;
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            }
            return;
        }

        /* internal  */onSetFloorCbChange(arg1: DataEvent): void
        {
            var loc1=arg1.data.selected;
            var loc2=CopyConfig.instance.getInfoByCode(arg1.data.code);
            if (loc1 && cache.pack.towerCopy.itemLength == 0) 
            {
                this.towerCopyView.showJumpFloorWin(loc2);
            }
            else 
            {
                if (loc1 && cache.pack.towerCopy.itemLength > 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60416));
                }
                CopyUtil.toWhichFloor = 0;
                this.towerCopyView.hideJumpFloorWin();
            }
            return;
        }

        /* internal  */onOpenTowerWarehouse(arg1=null): void
        {
            this.towerCopyView.showTowerWareHouse();
            return;
        }

        /* internal  */onJumpSuccess(arg1): void
        {
            this.onOpenTowerWarehouse();
            return;
        }

        /* internal  */onGetTowerWareHouseItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            var loc2=new Map<any, any> /* flash.utils.Dictionary */();
            loc2[loc1.uid] = loc1.itemAmount;
            GameProxy.packProxy.moveBag(loc1.posType, EPlayerItemPosType._EPlayerItemPosTypeBag, loc2, this.onMoveBagSuccess);
            return;
        }

        /* internal  */onGetAllTowerWareHouseItems(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=false;
            var loc4=null;
            if (cache.pack.towerCopy.itemLength <= 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69651));
            }
            else if (arg1.data != null) 
            {
                loc1 = arg1.data as Array<any>;
                loc2 = new Map<any, any> /* flash.utils.Dictionary */();
                loc3 = false;
                var loc5=0;
                var loc6=loc1;
                for(loc4 of loc6) 
                {
                    if (!DelegateSettingRule.isGettingItem(loc4)) 
                    {
                        continue;
                    }
                    loc2[loc4.uid] = loc4.itemAmount;
                    loc3 = true;
                }
                if (loc3) 
                {
                    GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTower, EPlayerItemPosType._EPlayerItemPosTypeBag, loc2);
                }
            }
            else 
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTower, EPlayerItemPosType._EPlayerItemPosTypeBag, null, this.onMoveBagSuccess);
            }
            return;
        }

        /* internal  */onMoveBagSuccess(arg1): void
        {
            MsgManager.showRollTipsMsg(Language.getString(69653));
            return;
        }

        /* internal  */onDestroyAllTowerWareHouseItems(arg1: DataEvent): void
        {
            GameProxy.packProxy.batchRemove(EPlayerItemPosType._EPlayerItemPosTypeTower);
            return;
        }

        /* internal  */onFeedAllTowerWareHouseItems(arg1: DataEvent): void
        {
            var loc1=cache.pack.mountsPackCache.equipEdMount;
            if (loc1) 
            {
                GameProxy.mountsProxy.feedMount(EPlayerItemPosType._EPlayerItemPosTypeRole, loc1.uid, EPlayerItemPosType._EPlayerItemPosTypeTower, arg1.data as Map<any, any> /* flash.utils.Dictionary */);
            }
            return;
        }

        /* internal  */onUpdateTowerWareHouseItems(arg1: DataEvent): void
        {
            this.towerCopyView.updateWareHouseItems();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._towerCopyView) 
            {
                this._towerCopyView = new TowerCopyView();
            }
            return this._towerCopyView;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMagicTowerInfo, this.onReceiveMagicTowerInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMagicTowerStory, this.onShowMagicTowerStory);
            NetDispatcher.addCmdListener(ServerCommand.Copy_LeaveMagicTowerCopy, this.onLeaveMagicTowerCopy);
            Dispatcher.addEventListener(EventName.CopySetFloorCbChange, this.onSetFloorCbChange);
            Dispatcher.addEventListener(EventName.CopyOpenTowerWarehouse, this.onOpenTowerWarehouse);
            Dispatcher.addEventListener(EventName.CopyMagicTowerJumpSuccess, this.onJumpSuccess);
            Dispatcher.addEventListener(GModuleEvent.CopyTowerWarehouse_Get, this.onGetTowerWareHouseItem);
            Dispatcher.addEventListener(GModuleEvent.CopyTowerWarehouse_GetAll, this.onGetAllTowerWareHouseItems);
            Dispatcher.addEventListener(GModuleEvent.CopyTowerWarehouse_destroyAll, this.onDestroyAllTowerWareHouseItems);
            Dispatcher.addEventListener(GModuleEvent.CopyTowerWarehouse_feedAll, this.onFeedAllTowerWareHouseItems);
            NetDispatcher.addCmdListener(ServerCommand.TowerWarehouseItemsUpdate, this.onUpdateTowerWareHouseItems);
            return;
        }

        protected /* override */ addListener(): void
        {
            Dispatcher.addEventListener(EventName.ChangeScene, this.onChangeSceneHandler);
            return;
        }

        protected /* override */ removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.ChangeScene, this.onChangeSceneHandler);
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            CopyUtil.toWhichFloor = 0;
            return;
        }

        public /* override */ stageResize(): void
        {
            if (this._magicTowerInfoPanel) 
            {
                this._magicTowerInfoPanel.updatePosition();
            }
            return;
        }

        public get towerCopyView(): TowerCopyView
        {
            if (!this._towerCopyView) 
            {
                this.initView();
            }
            return this._towerCopyView;
        }

        /* internal */ /* var */_towerCopyView: TowerCopyView;

        /* internal */ /* var */_magicTowerInfoPanel: MagicTowerInfoPanel;
    }
